package com.wgworkshop.pems.service;

import com.wgworkshop.pems.common.CommonUtils;
import com.wgworkshop.pems.common.enums.ClassArrangeStatusEnum;
import com.wgworkshop.pems.entity.ClassArrangeEntity;
import com.wgworkshop.pems.entity.StudentEntity;
import com.wgworkshop.pems.model.ClassArrangeByClassRecord;
import com.wgworkshop.pems.model.ClassArrangeModel;
import com.wgworkshop.pems.repository.ClassArrangeRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级安排服务接口实现类。
 * Created by wlc on 2017/10/11.
 */
@Service
public class ClassArrangeServiceImpl implements ClassArrangeService {
    @Autowired
    private ClassArrangeRepository classArrangeRepository;
    @Autowired
    private StudentService studentService;

    @Override
    public ClassArrangeModel save(ClassArrangeModel classArrangeModel) {
        Long studentId = classArrangeModel.getStudentId();
        studentService.checkExists(studentId);

        //将当前学生有效的班级安排记录的状态标记置为无效（形成历史记录）
        ClassArrangeEntity oldEffectiveRecord = classArrangeRepository.findFirstByStudentIdAndStatus(studentId, ClassArrangeStatusEnum.EFFECTIVE);
        if (oldEffectiveRecord != null) {
            oldEffectiveRecord.setStatus(ClassArrangeStatusEnum.INVALID);
            classArrangeRepository.save(oldEffectiveRecord);
        }

        ClassArrangeEntity classArrangeEntity = new ClassArrangeEntity();
        BeanUtils.copyProperties(classArrangeModel, classArrangeEntity);
        classArrangeEntity.setStatus(ClassArrangeStatusEnum.EFFECTIVE);

        StudentEntity studentEntity = studentService.getRepository().findOne(studentId);
        classArrangeEntity.setStudent(studentEntity);

        ClassArrangeEntity resultEntity = classArrangeRepository.save(classArrangeEntity);
        ClassArrangeModel resultModel = new ClassArrangeModel();
        BeanUtils.copyProperties(resultEntity, resultModel);
        resultModel.setStudentId(resultEntity.getStudent().getId());

        return resultModel;
    }

    @Override
    public List<ClassArrangeModel> saveAll(List<ClassArrangeModel> classArrangeModels) {
        List<ClassArrangeModel> resultModels = new ArrayList<>();
        for (ClassArrangeModel classArrangeModel : classArrangeModels) {
            resultModels.add(this.save(classArrangeModel));
        }
        return resultModels;
    }

    @Override
    public List<ClassArrangeModel> findByStudentId(Long id) {
        Assert.notNull(id, "id must not be null.");
        Sort.Order statusOrder = new Sort.Order(Sort.Direction.DESC, "status");
        Sort.Order startDateOrder = new Sort.Order(Sort.Direction.DESC, "startDate");
        Sort.Order idOrder = new Sort.Order(Sort.Direction.DESC, "id");
        List<Sort.Order> orderList = new ArrayList<>();
        orderList.add(statusOrder);
        orderList.add(startDateOrder);
        orderList.add(idOrder);
        Sort sort = new Sort(orderList);
        Iterable<ClassArrangeEntity> classArrangeEntities = classArrangeRepository.findByStudentId(id, sort);
        return CommonUtils.convertIterableToList(classArrangeEntities, ClassArrangeModel.class);
    }

    @Override
    public List<ClassArrangeByClassRecord> getArrangeByClass() {
        List<ClassArrangeByClassRecord> result = new ArrayList<>();
        classArrangeRepository.getArrangeByClass().forEach(result::add);
        return result;
    }
}
